home *** CD-ROM | disk | FTP | other *** search
- /* ulimit.c -- Setting resource usage for children of the shell. */
-
- /* Copyright (C) 1987,1989 Free Software Foundation, Inc.
-
- This file is part of GNU Bash, the Bourne Again SHell.
-
- Bash is free software; you can redistribute it and/or modify it under
- the terms of the GNU General Public License as published by the Free
- Software Foundation; either version 1, or (at your option) any later
- version.
-
- Bash is distributed in the hope that it will be useful, but WITHOUT ANY
- WARRANTY; without even the implied warranty of MERCHANTABILITY or
- FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
- for more details.
-
- You should have received a copy of the GNU General Public License along
- with Bash; see the file COPYING. If not, write to the Free Software
- Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
-
- #include <stdio.h>
- #include <sys/types.h>
- #include <sys/param.h>
- #include <errno.h>
- #include "shell.h"
-
- extern int errno;
-
- #if defined (HAVE_RESOURCE)
- #include <sys/time.h>
- #include <sys/resource.h>
- #else
- #include <sys/times.h>
- #endif
-
- #if defined (_POSIX_VERSION)
- #include <limits.h>
- #endif
-
- /* Check for the most basic synbols. If they aren't present, this
- system's <sys/resource.h> isn't very useful to us. */
- #if !defined RLIMIT_FSIZE
- # undef HAVE_RESOURCE
- #endif
-
- /* **************************************************************** */
- /* */
- /* Ulimit builtin and Hacks. */
- /* */
- /* **************************************************************** */
-
- /* Block size for ulimit operations. */
- #define ULIMIT_BLOCK_SIZE ((long)1024)
-
- #define u_FILE_SIZE 1
- #define u_MAX_BREAK_VAL 3
- #define u_PIPE_SIZE 4
- #define u_CORE_FILE_SIZE 6
- #define u_DATA_SEG_SIZE 8
- #define u_PHYS_MEM_SIZE 10
- #define u_CPU_TIME_LIMIT 12
- #define u_STACK_SIZE 14
- #define u_NUM_OPEN_FILES 16
-
- #ifndef RLIM_INFINITY
- #define RLIM_INFINITY 0x7fffffff
- #endif
-
- static long pipesize ();
-
- /* Report or set limits associated with certain per-process resources.
- See the help documentation in builtins.c for a full description.
- Chet Ramey & Brian Fox 3/13/89. */
- ulimit_builtin (list)
- register WORD_LIST *list;
- {
- long shell_ulimit ();
- register char *s;
- int c, setting, cmd, r = EXECUTION_SUCCESS;
- long current_limit, real_limit, limit = -1L;
- long block_factor;
-
- do
- {
- cmd = u_FILE_SIZE;
- setting = 0;
- block_factor = ULIMIT_BLOCK_SIZE;
-
- if (list)
- {
- s = list->word->word;
- if (s && (*s == '-'))
- {
- c = *++s;
-
- if (!c || *++s)
- goto error_case;
-
- list = list->next;
-
- switch (c)
- {
- case 'f':
- cmd = u_FILE_SIZE;
- break;
-
- #if defined (HAVE_RESOURCE)
- case 'a':
- print_all_limits ();
- return (EXECUTION_SUCCESS);
-
- case 'c': cmd = u_CORE_FILE_SIZE;
- break;
-
- case 'd': cmd = u_DATA_SEG_SIZE;
- break;
-
- case 'm': cmd = u_PHYS_MEM_SIZE;
- break;
-
- case 't':
- cmd = u_CPU_TIME_LIMIT;
- block_factor = 1; /* seconds */
- break;
-
- case 's':
- cmd = u_STACK_SIZE;
- break;
-
- case 'p':
- cmd = u_PIPE_SIZE;
- block_factor = 512;
- break;
-
- case 'n':
- cmd = u_NUM_OPEN_FILES;
- block_factor = 1;
- break;
-
- #endif /* HAVE_RESOURCE */
- default:
- error_case:
- #if ! defined (HAVE_RESOURCE)
- builtin_error("usage: ulimit [-f] [new limit]");
- #else
- builtin_error("usage: ulimit [-acmdstfpn] [new limit]");
- #endif
- return EXECUTION_FAILURE;
- }
- }
-
- /* If an argument was supplied for the command, then we want to
- set the limit. */
- if (list)
- {
- s = list->word->word;
- list = list->next;
- if (sscanf (s, "%ld", &limit) != 1)
- {
- if (strcmp (s, "unlimited") == 0)
- limit = RLIM_INFINITY;
- else
- {
- builtin_error ("bad non-numeric arg `%s'", s);
- return (EXECUTION_FAILURE);
- }
- }
- setting++;
- }
- }
-
- if (limit == RLIM_INFINITY)
- block_factor = 1;
-
- real_limit = limit * block_factor;
-
- current_limit = shell_ulimit (cmd, real_limit, 0);
-
- if (setting)
- {
- #if !defined (HAVE_RESOURCE)
- if ((current_limit < (real_limit)) && (getuid() != 0))
- {
- builtin_error ("cannot raise limit");
- return (EXECUTION_FAILURE);
- }
- #endif
-
- if (shell_ulimit (cmd, real_limit, 1) == -1)
- {
- builtin_error ("cannot raise limit: %s", strerror(errno));
- r = EXECUTION_FAILURE;
- }
- else
- r = EXECUTION_SUCCESS;
-
- return (r);
- }
- else
- {
- if (current_limit != RLIM_INFINITY)
- printf ("%ld\n", (current_limit / block_factor));
- else
- printf ("unlimited\n");
- }
- }
- while (list);
-
- return (EXECUTION_SUCCESS);
- }
-
- /* The ulimit that we call from within Bash. Extended to handle
- more resources by Chet Ramey (chet@cwjcc.cwru.edu).
- WHICH says which limit to twiddle; SETTING is non-zero if NEWLIM
- contains the desired new limit. Otherwise, the existing limit is
- returned. */
- long
- shell_ulimit (which, newlim, setting)
- int which, setting;
- long newlim;
- {
- #if defined (HAVE_RESOURCE)
- struct rlimit limit;
- #endif
-
- switch (which)
- {
-
- case u_FILE_SIZE:
- if (!setting)
- {
- #if !defined (HAVE_RESOURCE)
- /* ulimit () returns a number that is in 512 byte blocks, thus we
- must multiply it by 512 to get back to bytes. This is false
- only under HP/UX 6.x. */
- long result;
-
- result = ulimit (1, 0l);
-
- #if defined (hpux) && !defined (_POSIX_VERSION)
- return (result);
- #else
- return (result * 512);
- #endif /* Hpux 6.x */
- #else
- if (getrlimit (RLIMIT_FSIZE, &limit) != 0)
- return ((long) -1);
- return (limit.rlim_cur);
- #endif
- }
- else
- {
- #if !defined (HAVE_RESOURCE)
- return (ulimit (2, newlim / 512L));
- #else
- getrlimit (RLIMIT_FSIZE, &limit);
- limit.rlim_cur = newlim;
- return (setrlimit (RLIMIT_FSIZE, &limit));
- #endif
- }
-
- case u_MAX_BREAK_VAL:
- #if !defined (HAVE_RESOURCE)
- if (!setting)
- return (ulimit (3, 0L));
- else
- return ((long) -1);
- #else
- if (!setting)
- {
- if (getrlimit (RLIMIT_DATA, &limit) != 0)
- return ((long) -1);
- return (limit.rlim_cur);
- }
- else
- {
- getrlimit (RLIMIT_DATA, &limit);
- limit.rlim_cur = newlim;
- return (setrlimit (RLIMIT_DATA, &limit));
- }
- #endif
-
- #if defined (HAVE_RESOURCE)
- /* You can't get or set the pipe size with getrlimit, so we have to
- cheat. */
- case u_PIPE_SIZE:
- if (setting)
- {
- errno = EINVAL;
- return ((long) -1);
- }
- return (pipesize ());
-
- case u_CORE_FILE_SIZE:
- if (!setting)
- {
- if (getrlimit (RLIMIT_CORE, &limit) != 0)
- return ((long) -1);
- return (limit.rlim_cur);
- }
- else
- {
- getrlimit (RLIMIT_CORE, &limit);
- limit.rlim_cur = newlim;
- return (setrlimit (RLIMIT_CORE, &limit));
- }
-
- case u_DATA_SEG_SIZE:
- if (!setting)
- {
- if (getrlimit (RLIMIT_DATA, &limit) != 0)
- return (long) -1;
- return (limit.rlim_cur);
- }
- else
- {
- getrlimit (RLIMIT_DATA, &limit);
- limit.rlim_cur = newlim;
- return (setrlimit (RLIMIT_DATA, &limit));
- }
-
- case u_PHYS_MEM_SIZE:
- if (!setting)
- {
- if (getrlimit (RLIMIT_RSS, &limit) != 0)
- return ((long) -1);
- return (limit.rlim_cur);
- }
- else
- {
- getrlimit (RLIMIT_RSS, &limit);
- limit.rlim_cur = newlim;
- return (setrlimit (RLIMIT_RSS, &limit));
- }
-
- case u_CPU_TIME_LIMIT:
- if (!setting)
- {
- if (getrlimit (RLIMIT_CPU, &limit) != 0)
- return ((long) -1);
- return (limit.rlim_cur);
- }
- else
- {
- getrlimit (RLIMIT_CPU, &limit);
- limit.rlim_cur = newlim;
- return (setrlimit (RLIMIT_CPU, &limit));
- }
-
- case u_STACK_SIZE:
- if (!setting)
- {
- if (getrlimit (RLIMIT_STACK, &limit) != 0)
- return ((long) -1);
- return (limit.rlim_cur);
- }
- else
- {
- getrlimit (RLIMIT_STACK, &limit);
- limit.rlim_cur = newlim;
- return (setrlimit (RLIMIT_STACK, &limit));
- }
-
- case u_NUM_OPEN_FILES:
- if (setting)
- {
- #if defined (RLIMIT_NOFILE)
- getrlimit (RLIMIT_NOFILE, &limit);
- limit.rlim_cur = newlim;
- return (setrlimit (RLIMIT_NOFILE, &limit));
- #else /* !RLIMIT_NOFILE */
- errno = EINVAL;
- return ((long) -1);
- #endif /* !RLIMIT_NOFILE */
- }
- else
- return ((long) getdtablesize ());
-
- #endif /* HAVE_RESOURCE */
-
- default:
- errno = EINVAL;
- return ((long) -1);
- }
- }
-
- #include "pipesize.h"
-
- static long
- pipesize ()
- {
- #if defined (PIPE_BUF)
- /* This is defined on Posix systems. */
- return ((long) PIPE_BUF);
- #else
- # if defined (PIPESIZE)
- /* This is defined by running a program from the Makefile. */
- return ((long) PIPESIZE);
- # else
-
- errno = EINVAL;
- return ((long) -1);
- # endif /* PIPESIZE */
- #endif /* PIPE_BUF */
- }
-
- #if defined (HAVE_RESOURCE)
-
- #if !defined (RLIM_NLIMITS)
- #define RLIM_NLIMITS 6 /* Number of resource limits. */
- #endif
-
- typedef struct {
- int parameter; /* Parameter to pass to getrlimit (). */
- int block_factor; /* Blocking factor for specific limit. */
- char *description; /* Descriptive string to output. */
- } BSD_RESOURCE_LIMITS;
-
- BSD_RESOURCE_LIMITS limits[RLIM_NLIMITS + 1] = {
- { RLIMIT_CPU, 1, "cpu time (seconds)" },
- { RLIMIT_RSS, 1024, "max memory size (kbytes)" },
- { RLIMIT_DATA, 1024, "data seg size (kbytes)" },
- { RLIMIT_STACK, 1024, "stack size (kbytes)" },
- { RLIMIT_FSIZE, 1024, "file size (blocks)" },
- { RLIMIT_CORE, 1024, "core file size (blocks)" },
- { 0, 0, (char *)NULL }
- };
-
- print_all_limits ()
- {
- register int i;
- struct rlimit rl;
- long limit;
-
- for (i = 0; i < RLIM_NLIMITS && limits[i].description; i++)
- {
- getrlimit (limits[i].parameter, &rl);
- limit = rl.rlim_cur;
- printf ("%-25s", limits[i].description);
- if (limit == RLIM_INFINITY)
- printf ("unlimited\n");
- else
- printf ("%ld\n", limit / limits[i].block_factor);
- }
- printf ("%-25s%ld\n", "pipe size (512 bytes)", (pipesize () / 512));
- printf ("%-25s%ld\n", "open files", getdtablesize ());
- }
-
- #endif /* HAVE_RESOURCE */
-